<?php
/*
	评论管理
*/
require_once 'init.php';
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
//定义表名
define("TABLENAME","leavelmessage");

//删除
if( isset($_GET['del_id']) ){
	$_GET['del_id'] = trim($_GET['del_id'],',');
	$db->sql="delete from ".TABLENAME." where messageid in ({$_GET['del_id']})";
	$db->doExecute();
	header("Location:{$_SERVER['HTTP_REFERER']}");
	exit();
	//header("Location:common.php?{$_SERVER['QUERY_STRING']}");
}

//修改


//提交处理
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	$pic_id=$_POST['pic_id'];
	$description=htmlspecialchars($_POST['adminReply']);
	//$updater_2 = $_POST['updater_2'];
	//echo $db->sql="UPDATE ".TABLENAME." SET message = '{$description}' , messageid = {$pic_id} , `date` = '"+date("Y-m-d")+"' , openid = 'admin', figureurl = 'http://ctc.qzonestyle.gtimg.cn/qzonestyle/act/qzone_app_img/app100626394_100626394_100.png',nickname='admin',isread=0  WHERE id = ".$pic_id;
	$db->sql="INSERT INTO `leavelmessage`(`messageid`,`message`,`date`,`openid`,`figureurl`,`nickname`,`isread`) VALUES({$pic_id},'{$description}','".date("Y-m-d")."','admin','appearance/upin_logo.png','admin',0)";
	$db->doExecute();
}

//条件
$where = "WHERE id = messageid ";
$where.= !empty($_GET['startTime'])	?	" AND date>='{$_GET['startTime']}' ":"";
$where.= !empty($_GET['endTime'])	?	" AND date<='{$_GET['endTime']}' "	:"";
if(!empty($_GET['nick'])){
	$where .= " and nickname like ".($_GET['is_check'] == 'true'?"'{$_GET['nick']}'":"'%{$_GET['nick']}%'");
}


//排序PublishDate Updater Shared Collected
if(!empty($_GET['sort'])){
	$sortarray = explode('_', $_GET['sort']);
	$order = "p.".$sortarray[0]." ".$sortarray[1].",";
}else{
	$order = '';
}
//总条数
$db->sql="SELECT count(id) count_id FROM ".TABLENAME." ".$where;
$count = $db->doResultOne();

//分页
if(empty($_GET['page'])){
	$page = 1;
}else{
	$page=$_GET['page'];
}
if(empty($_GET['pagesize'])){
	$pagesize = PAGESIZE;
}else{
	$pagesize = $_GET['pagesize'];
}


$pageInfo = page($count, $pagesize, $page);
$maxPage = $pageInfo['maxPage'];
$start = $pageInfo['start'];
$pages = $pageInfo['pages'];

$db->sql="SELECT * FROM ".TABLENAME.' '.$where." ORDER BY ".$order." id DESC LIMIT ".$start.",".$pagesize;
$pictures = $db->doResultList2();

//回复
foreach ($pictures as $key => $item){
	$db->sql="SELECT * FROM leavelmessage WHERE messageid = {$item['id']} AND id != messageid ";
	$pictures[$key]['childrenArr'] = $db->doResultList2();
}
//var_dump($pictures);die();
$url= "?startTime=".$_GET['startTime']."&endTime=".$_GET['endTime']."&nick=".$_GET['nick']."&pagesize=".$_GET['pagesize']."&is_check=".$_GET['is_check'];

//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;} 
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simpla Admin</title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css"
	media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css"
	media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css"
	media="screen" />
<!-- Colour Schemes
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
		-->
<!-- Internet Explorer Fixes Stylesheet -->
<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
<!--                       Javascripts                       -->
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>

<script type="text/javascript">
$(function($) {
	$(".editbutton").click(function(){
		$("#pic_id").val($(this).attr('id'));
		//获取描述
		$.ajax({
			   type: "GET",
			   url: "getDesc.php",
			   data: "id="+$(this).attr('id'),
			   dataType: "html",
			   success: function(msg){
				   $("#description").html(msg);
				   $("#message_click").click();
			   }
		});
		
	});
	//删除
	$(".delbutton").click(function(){
		var id = $(this).parents('tr').data('id');
		if(window.confirm('你确定要删除该记录！')){
			//删除数据
			$.ajax({
				type: "GET",
				url: "leavelmessage.php",
				data: "del_id="+id,
				async: false,
				success: function(){
					alert('删除成功');
				}
			});
			$("tr[data-id='"+id+"']").remove();
		}
	});
	//多数据操作
	$(".del_array").click(function(){
		var text="";  
		$("input[name=picSel]").each(function() {  
			if ($(this).attr("checked")) {
				text += ","+$(this).val();  
			}
		});
		if(window.confirm('你确定要删除记录！')){
			window.location.replace('leavelmessage.php?del_id='+text);
		}
	});
	//回复
	$(".huifu").click(function(){
		var id = $(this).data("id");
		$("#pic_id").val($(this).data("id"));
		$("#message_click").click();
	});
});

</script>

<!-- jQuery Configuration -->
<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery my97datepicker -->
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
</head>
<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		<a href="#messages" rel="modal" id="message_click"></a>
		<div id="messages" style="display: none;">
			<!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal"  -->

			<p>
				<strong>回复</strong>
			</p>
			<form action="<?php echo $url."&page=".$page;?>" method="post" style="text-align: right;">
				<fieldset>
					<textarea class="textarea" id="adminReply" name="adminReply" cols="79" rows="5"></textarea>
					<input id="pic_id" name="pic_id" type="hidden" value="" />
					<input class="button" type="submit" value="提交" />
					<a href="javascript:void(0);" onclick="$(this).parents('#facebox').hide();">关闭</a>
				</fieldset>
			</form>

		</div>
		<!-- End #messages -->
		
		<div id="main-content">
			<!-- Main Content Section with everything -->
			<!-- Page Head -->
			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-content">
					<form method="get" action="">
						时间：
						<input class="text-input" type="text" id="startTime" name="startTime" onClick="WdatePicker()" value="<?php echo $_GET['startTime'];?>" size="10"/> 
						-
						<input class="text-input" type="text" id="endTime" name="endTime" onClick="WdatePicker()" value="<?php echo $_GET['endTime'];?>" size="10"/>
						&nbsp;&nbsp;&nbsp;用户昵称：
						<input class="text-input" name="nick" value="<?php echo $_GET['nick']?>" size="20" type="text"/>
						全名匹配：<inpu class="text-input"t name="is_check" value="true" type="checkbox" <?php echo $_GET['is_check']=='true'?'checked':'';?>/>
						&nbsp;&nbsp;&nbsp;
						每页<input class="text-input" name="pagesize" type="text" size="2" value="<?php echo $pagesize;?>"/>条
						&nbsp;&nbsp;&nbsp;
						<button class="button" id="zhixing" type="submit">搜索</button>
						<input type="hidden" name="page" value="<?php echo $page;?>"/>
					</form>
					
					<div class="clear" style="border-bottom: 1px solid #ddd;margin:5px 0;"></div>
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>
							<thead>
								<tr>
									<td colspan="6">
										<div class="bulk-actions align-left">
											<button class="button del_array" >删除</button>
											<?php if($yldate): ?>
											<a target="_blank" href="http://rc.qzone.qq.com/myhome/100626394?via=QZ.BLOGFEED.BLOG&date=<?php echo $yldate;?>"><button class="button yulan" >今日预览</button></a>
										    <?php endif;?>
										</div>
										<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="<?php echo $url;?>&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo $url."&page=".$pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="<?php echo $url."&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo $url."&page=".$pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo $url."&page=".$maxPage;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<div class="clear"></div>
									</td>
								</tr>
							</thead>
							<thead>
								<tr>
									<th><input class="check-all" type="checkbox" /></th>
									<th>编号</th>
									<th>用户</th>
									<th>日期</th>
									<th width="200" style="width: 40%;">内容</th>
									
									<th>操作</th>
								</tr>

							</thead>

							<tbody>
							<?php if(count($pictures)>0):?>
							<?php foreach ($pictures as $key=>$item):?>
								<tr data-id="<?php echo $item['id'];?>">
									<td><input type="checkbox" name="picSel" value="<?php echo $item['id'];?>"/></td>
									<td><?php echo $item['id'];?></td>									
									<td><img src="<?php echo $item['figureurl']?>" width="50"><br/><?php echo $item['nickname']?></td>
									<td><?php echo $item['date']?></td>
									<td width="200"><?php echo $item['message']?></td>
									
									<td>
										<!--<a title="Edit" class="editbutton"><img src="resources/images/icons/pencil.png" alt="Edit" /></a>-->
										<a title="回复" class="huifu" data-id="<?php echo $item['id'];?>" style="cursor: pointer;">回复</a>
										<a title="Delete" class="delbutton"><img src="resources/images/icons/cross.png" alt="Delete" /></a>
									</td>
								</tr>
								<?php foreach ($item['childrenArr'] as $key2=>$item2):?>
								<tr data-id="<?php echo $item['id'];?>">
									<td></td>
									<td></td>									
									<td><?php echo $item2['nickname']?></td>
									<td><?php echo $item2['date']?></td>
									<td width="200"><?php echo $item2['message']?></td>
									
									<td>
										<!--<a title="Edit" class="editbutton"><img src="resources/images/icons/pencil.png" alt="Edit" /></a>-->
									</td>
								</tr>
								<?php endforeach;?>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="9" style="text-align: center;">没有图片</td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="6">
										<div class="bulk-actions align-left">
											<button class="button del_array" >删除</button>
										</div>
										<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="<?php echo $url;?>&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo $url."&page=".$pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="<?php echo $url."&page=".$item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo $url."&page=".$pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo $url."&page=".$maxPage;?>" title="Last Page">Last &raquo;</a>
											total : <?php echo $maxPage;?>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<div class="clear"></div>
									</td>
								</tr>
							</tfoot>

						</table>

					</div>
					<!-- End #tab1 -->


				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
